package com.hunan.api.catv.service.impl.ding;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.hunan.api.catv.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class DingService extends ResultService {

    @Autowired
    RestTemplate restTemplate;

    public void dingNotify(String title, String message, String server) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json");
            JsonObject msg = new JsonObject();
            msg.addProperty("msgtype", "markdown");
            JsonObject markdown = new JsonObject();
            markdown.addProperty("text", "[" + message + "]");
            markdown.addProperty("title", title);
            msg.add("markdown", markdown);
            HttpEntity<String> requestEntity = new HttpEntity<String>(msg.toString(), headers);
            restTemplate.postForObject(server, requestEntity, JsonObject.class);
        } catch (Exception e) {
            log.info("ding error：{}", e);
        }
    }

    public void atNotify(String title, String message, String atPhone, String server) {
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json");
            JsonObject msg = new JsonObject();
            msg.addProperty("msgtype", "markdown");
            JsonObject markdown = new JsonObject();
            markdown.addProperty("text", "[" + message + "]" + "@" + atPhone);
            markdown.addProperty("title", title);
            msg.add("markdown", markdown);
            JsonObject at = new JsonObject();
            at.addProperty("isAtAll", false);
            JsonArray atMobiles = new JsonArray();
            atMobiles.add(atPhone);
            at.add("atMobiles", atMobiles);
            msg.add("at", at);
            HttpEntity<String> requestEntity = new HttpEntity<String>(msg.toString(), headers);
            restTemplate.postForObject(server, requestEntity, JsonObject.class);
        } catch (Exception e) {
            log.info("ding error：{}", e);
        }
    }
}
